/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.server;

import java.io.File;

import net.bioteam.appweb.Config;
import net.bioteam.appweb.gwt.apps.clientandserver.Constant;

import org.apache.commons.io.FilenameUtils;

public class UserFileConverter
{
	public static String DATA_ROOT = "/"+Constant.server_file_user_name+"/"+Constant.data_name;
	public static final String JOB_ROOT="/"+Constant.server_file_user_name+"/"+Constant.job_name; 
	public static final String PUBLIC_ROOT="/"+Constant.server_file_public_name;
	
	
	public static String clientPathToRealPath(String cPath, String username) throws UnsupportedUserFileTypeException
	{
		if (isDataFile(cPath))
		{
			return dataClientPathToRealPath(cPath, username);
		} else if (isJobFile(cPath))
		{
			return jobClientPathToRealPath(cPath, username);
		} else if (isPublicFile(cPath))
		{
			return publicClientPathToRealPath(cPath);
		}
		throw new UnsupportedUserFileTypeException("Client path "+cPath+" is not supported");
	
	}
	
	
	
	public static String realPathToClientPath(String rPath, String username) throws UnsupportedUserFileTypeException
	{
		if (rPath.startsWith(Config.USER_FILE))
		{
			if (rPath.startsWith(Config.USER_FILE+File.separator+Config.PUBLIC_FILE_DIR))
			{
				return publicRealPathToClientPath(rPath);
			} else
			{
				return dataRealPathToClientPath(rPath, username);
			}
		} else if (rPath.startsWith(Config.topWorkdir))
		{
			return jobRealPathToClientPath(rPath, username);
		} 
		
		throw new UnsupportedUserFileTypeException("file path"+rPath+" is not supported");
	}
	
	public static String dataClientPathToRealPath(String cPath, String username)
	{
		return Config.USER_FILE + File.separator + username + FilenameUtils.separatorsToSystem(cPath.substring(DATA_ROOT.length()));
	}

	public static String dataRealPathToClientPath(String rPath, String username)
	{
		String userdir = Config.USER_FILE + File.separator + username;
		return DATA_ROOT + FilenameUtils.separatorsToUnix(rPath.substring(userdir.length()));
	}
	

	public static String jobClientPathToRealPath(String cPath, String username)
	{
		return Config.topWorkdir + File.separator + username + FilenameUtils.separatorsToSystem(cPath.substring(JOB_ROOT.length()));
	}

	public static String jobRealPathToClientPath(String rPath, String username)
	{
		String userdir = Config.topWorkdir + File.separator + username;
		return JOB_ROOT + FilenameUtils.separatorsToUnix(rPath.substring(userdir.length()));
	}
	
	public static String publicClientPathToRealPath(String cPath)
	{
		return Config.USER_FILE + File.separator + Config.PUBLIC_FILE_DIR + FilenameUtils.separatorsToSystem(cPath.substring(PUBLIC_ROOT.length()));
	}

	public static String publicRealPathToClientPath(String rPath)
	{
		String userdir = Config.USER_FILE + File.separator + Config.PUBLIC_FILE_DIR;
		return PUBLIC_ROOT + FilenameUtils.separatorsToUnix(rPath.substring(userdir.length()));
	}
	
	public static boolean isJobFile(String cPath)
	{
		return cPath.startsWith(JOB_ROOT);
	}
	
	public static boolean isDataFile(String cPath)
	{
		return cPath.startsWith(DATA_ROOT);
	}
	
	public static boolean isPublicFile(String cPath)
	{
		return cPath.startsWith(PUBLIC_ROOT);
	}

}
